package com.mycp.privacy.transform
import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformInvocation
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.internal.pipeline.TransformManager
import com.didiglobal.booster.gradle.*
import com.didiglobal.booster.transform.AbstractKlassPool
import com.didiglobal.booster.transform.Transformer
import com.mycp.privacy.DelegateTransformInvocation
import org.gradle.api.Project

/**
 * @author: liujie
 * @date: 2022/8/23 上午10:16
 * @description:
 */
open class BaseTransform protected constructor(val project: Project) : Transform() {

    internal open val transformers = listOf<Transformer>()

    internal val verifyEnabled = false

    private val android: BaseExtension = project.getAndroid()

    private lateinit var androidKlassPool: AbstractKlassPool

    init {
        project.afterEvaluate {
            androidKlassPool = object : AbstractKlassPool(android.bootClasspath) {}
        }
    }

    val bootKlassPool: AbstractKlassPool
        get() = androidKlassPool

    override fun getName() = this.javaClass.simpleName

    override fun isIncremental() = !verifyEnabled

    override fun isCacheable() = !verifyEnabled

    override fun getInputTypes(): MutableSet<QualifiedContent.ContentType> = TransformManager.CONTENT_CLASS

    override fun getScopes(): MutableSet<in QualifiedContent.Scope> = TransformManager.SCOPE_FULL_PROJECT

    override fun getReferencedScopes(): MutableSet<in QualifiedContent.Scope> = TransformManager.SCOPE_FULL_PROJECT

    final override fun transform(invocation: TransformInvocation) {
        DelegateTransformInvocation(invocation, this).apply {
            println("开始-----------------------------------------------------------------------------------------")
            println("开始-----------------------------------------------------------------------------------------")
            println("开始-----------------------------------------------------------------------------------------")
            println("开始-----------------------------------------------------------------------------------------")
            println("开始-----------------------------------------------------------------------------------------")
            outputProvider?.deleteAll()
            doFullTransform()
        }
    }
}